package com.cjh.web;

import com.cjh.pojo.Book;
import com.cjh.pojo.Cart;
import com.cjh.pojo.CartItem;
import com.cjh.service.BookService;
import com.cjh.service.impl.BookServiceImpl;
import com.cjh.utils.WebUtils;
import com.google.gson.Gson;
import com.sun.xml.internal.bind.v2.model.core.ID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: huahua
 * @Date: 2020-09-16 22:23
 */
public class CartServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();

    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        int count = WebUtils.parseInt(req.getParameter("count"), 1);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.updateCount(id, count);
            //重定向到从哪儿来回哪儿去
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    /**
     * 清空购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.clear();
            //重定向到从哪儿来回哪儿去
            resp.sendRedirect(req.getHeader("Referer"));

        }
    }

    /**
     * 删除购物车中的选项
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.deleteItem(id);
            //重定向到从哪儿来回哪儿去
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    /**
     * 加入购物车
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数 商品编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //调用bookservice。queryBookById（id）得到图书信息
        Book book = bookService.queryBookById(id);
        //把图书信息转化为CartItem对象
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        //调用cart.addItem添加商品项
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        cart.addItem(cartItem);
        System.out.println(cart);
        //重定向到从哪儿来回哪儿去
        resp.sendRedirect(req.getHeader("Referer"));

        req.getSession().setAttribute("lastName", cartItem.getName());
    }

    /**
     * ajax请求加入购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void ajaxAddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数 商品编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //调用bookservice。queryBookById（id）得到图书信息
        Book book = bookService.queryBookById(id);
        //把图书信息转化为CartItem对象
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        //调用cart.addItem添加商品项
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        cart.addItem(cartItem);
        System.out.println(cart);
        req.getSession().setAttribute("lastName", cartItem.getName());
        Map<String, Object> result = new HashMap<>();
        result.put("totalCount", cart.getTotalCount());
        result.put("lastName", cartItem.getName());
        Gson gson = new Gson();
        String s = gson.toJson(result);
        resp.getWriter().write(s);
    }
}
